package com.pinyoumei.app.activity.wallet;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.aliyun.utils.AppUtils;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.wallet.WalletRechargeBean;
import com.pinyoumei.app.utils.SPUtils;

import java.util.List;

public class BackCardAdapter extends BaseAdapter {

    private Context context;
    private List<BankCardListBean.BankCardListDto> datas;

    public BackCardAdapter(Context context){
        this.context = context;

    }

    public void setData(List<BankCardListBean.BankCardListDto> datas){
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.back_card_item, parent, false);
            viewHolder = new ViewHolder(convertView);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        BankCardListBean.BankCardListDto dto = datas.get(position);
        String acNo = dto.getBankAcNo();
        String acNoSub = "";
        if (!TextUtils.isEmpty(acNo)){
            acNoSub = acNo.substring(acNo.length()-4,acNo.length());
        }else{
            acNoSub = "";
        }
        getBankIcon(viewHolder,dto.getBankName());
        viewHolder.bciBox.setChecked(dto.isBox());
        viewHolder.bciName.setText(dto.getBankName()+"　储蓄卡("+acNoSub+")");


        return convertView;
    }

    /**
     * ViewHolder,view缓存
     */
    static class ViewHolder {
        private ImageView bciIcon;
        private TextView bciName;
        private CheckBox bciBox;
        public ViewHolder(View view) {
            bciIcon = view.findViewById(R.id.bci_icon);
            bciName = view.findViewById(R.id.bci_name);
            bciBox = view.findViewById(R.id.bci_box);
        }
    }

    private void getBankIcon(ViewHolder viewHolder,String bankname){
        for (int i = 0; i < SPUtils.bankName.length; i++) {
            if(bankname.equals(SPUtils.bankName[i])){
                viewHolder.bciIcon.setBackgroundResource(SPUtils.bankIcon[i]);
            }
        }
    }
}
